package main

import (
	"fmt"
	image "github.com/star-find-cloud/star-chat/image"
	image_repo "github.com/star-find-cloud/star-chat/image/repo"
	image_srv "github.com/star-find-cloud/star-chat/image/service"
	"github.com/star-find-cloud/star-chat/interface/http"
	"github.com/star-find-cloud/star-chat/pkg/database"
	log "github.com/star-find-cloud/star-chat/pkg/logger"
	"github.com/star-find-cloud/star-chat/user"
	userPb "github.com/star-find-cloud/star-chat/user/pb"
	userRepo "github.com/star-find-cloud/star-chat/user/repo"
	userService "github.com/star-find-cloud/star-chat/user/service"
	"google.golang.org/grpc"
	"net"
)

func main() {
	db, err := database.NewMySQL()
	if err != nil {
		log.AppLogger.Errorf("get mysql connect error: %v", err)
		fmt.Printf("get mysql connect error: %v", err)
		return
	}
	cache, err := database.NewRedis()
	if err != nil {
		log.AppLogger.Errorf("get redis connect error: %v", err)
		fmt.Printf("get redis connect error: %v", err)
		return
	}

	userRep := userRepo.NewUserRepo(db, cache)
	userSrv := userService.NewUserServiceImpl(userRep)
	userHandler := user.NewUserHandler(userSrv)
	userGrpcServer := user.NewUserServer(userSrv)

	//imgRep := image_repo.NewImageRepo(db, cache)
	//imgSrv := image_srv.NewImageOssService(imgRep, ossClient)
	//imgHandler := image.NewImageHandler(imgSrv)

	fmt.Println("初始化完成")
	var r = http.InitRouter(userHandler)

	fmt.Println("gin 配置完成")
	fmt.Println("正在启动服务器...")

	grpcServer := grpc.NewServer()
	userPb.RegisterUserServiceServer(grpcServer, userGrpcServer)

	// 监听端口
	lis, err := net.Listen("tcp", ":50051")
	if err != nil {
		fmt.Printf("failed to listen: %v", err)
		return
	}

	fmt.Println("Server starting on port 50051...")

	// 启动服务
	if err := grpcServer.Serve(lis); err != nil {
		fmt.Printf("failed to serve: %v", err)
		return
	}

	if err := r.Run(":8080"); err != nil {
		fmt.Printf("服务器启动失败: %v\n", err)
		panic(err)
	}
}
